package com.bingxue.edu.tools;

import com.bingxue.edu.common.enums.EnrollmentStatusEnum;
import com.bingxue.edu.management.education.model.entity.Enrollment;
import com.bingxue.edu.management.education.service.EnrollmentService;
import com.bingxue.edu.management.shop.model.entity.SellRecord;
import com.bingxue.edu.management.shop.service.SellRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;

/**
 * 处理重复缴费导致的订单状态异常
 * @author panbo
 * @since 2024/11/23
 */
@SpringBootTest
public class SellRecordFixedTool {

    @Autowired
    private EnrollmentService enrollmentService;

    @Autowired
    private SellRecordService sellRecordService;

//    @Test
//    @Transactional(rollbackFor = Exception.class)
    public void fixed() {
        List<Enrollment> list = enrollmentService.queryChain()
                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                .where(CLASS_INFO.CLASS_YEAR.eq(2025))
                .and(ENROLLMENT.SELLER_ID.isNotNull())
                .and(ENROLLMENT.STATUS.eq(EnrollmentStatusEnum.NORMAL))
                .list();
        for (Enrollment enrollment : list) {
            SellRecord record = sellRecordService.getByEnrollmentId(enrollment.getId());
            if (record == null) {
                sellRecordService.createEnrollSellRecord(enrollment, enrollment.getSellerId());
            }
        }
    }


}
